package morenoapp.tools;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author dmitry.mamonov
 */
public class InternationalisationScanner {
    public static void main(String[] args) throws IOException {
        trace(new File("src")); //NOI18N
        //Thread.dumpStack();

//        for(MorenoSex s:MorenoSex.values()){
//            System.out.println("color: "+MorenoUtil.colorToHtml(s.getColor()));
//        }
    }

    public static void trace(File f) throws IOException {
        //System.out.println("file: " + f);
        if (f.isDirectory()) {
            for (File subfile : f.listFiles()) {
                trace(subfile);
            }
        } else {
            if (f.getName().endsWith(".java") && f.getAbsolutePath().contains("morenoapp")) { //NOI18N
                findNotI18nLiterals(f);
            }
        }
    }

    private static void findNotI18nLiterals(File f) throws IOException {
        int lineNumber = 0;
        String prevLine = "";
        for (String line : (List<String>) FileUtils.readLines(f, "UTF-8")) { //NOI18N
            lineNumber++;
            
            boolean stringLiteral = line.replaceAll("\"\"", "").contains("\""); //pass empty literals //NOI18N
            boolean i18n = line.matches(".*//\\s*I18N.*") || line.contains("I18N.get"); //pass internationalized literals //NOI18N
            boolean noi18n = line.contains("NOI18N"); //pass literals not need to be internationalized //NOI18N
            boolean prevNoi18n = prevLine.trim().matches("\\A//\\s*NOI18N\\Z"); //pass if noi18n token presented in previous line as single //NOI18N
            boolean annotation = line.trim().startsWith("@"); //pass literals in annotations //NOI18N
            boolean comment = line.trim().startsWith("//"); //pass literals in comments //NOI18N
            boolean beansBingdingException=line.contains(".ELProperty."); //excpetion for beans binding statement - due to bug in netbeans code generator
            boolean tahomaException=line.contains("\"Tahoma\"");//due to bug in netbeans code generator we pass "Tahoma" string as localized
            if (stringLiteral && !i18n && !noi18n && !prevNoi18n && !annotation && !comment && !beansBingdingException && !tahomaException) {
                System.out.printf("%30s @ %04d: %s\n", f.getName().replace(".", " "), lineNumber, line.trim()); //NOI18N
            }
            prevLine = line;
        }
    }
}
